package com.stu.stusystem.config;

import com.stu.stusystem.mapper.system.StuMapper;
import com.stu.stusystem.model.system.StuUser;
import com.stu.stusystem.model.em.Jurisdiction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author cxt
 * @date 2020/9/8
 * <p>
 * 在项目启动后执行：向数据库中写入初始化数据
 */
@Component
@Slf4j
public class ApplicationRunnerImpl implements ApplicationRunner {
    private StuMapper stuMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        StuUser admin = stuMapper.selectOne(new StuUser("admin"));
        if (admin == null) {
            StuUser stuUser = new StuUser("INIT20200908", "admin", "超级管理员", "usZen62Aorxe0w4a0f8FRsCQggA=", Jurisdiction.ADMIN);
            stuUser.setCreateBy("admin");
            stuUser.setCreateTime(new Date());
            int insert = stuMapper.insert(stuUser);
            if (insert != 1) {
                log.debug("数据库插入管理员信息失败！");
                throw new Exception("数据库初始化失败！请联系管理员！");
            }
        }
    }

    @Autowired
    public void setStuUserMapper(StuMapper stuMapper) {
        this.stuMapper = stuMapper;
    }
}
